import {
    DBPost
} from '../../../db/DBPost.js';
Page({
    data: {
        morehide: true,
        speak: false,
        currentAudio: '',
        inputvalue: '',
        chooseFiles:[]
    },
    onLoad: function(options) {
        var postId = options.id;
        this.dbPost = new DBPost(postId);
        var comments = this.dbPost.getCommentData();
        this.setData({
            comments: comments
        })
    },
    showmore: function() {
        this.setData({
            morehide: !this.data.morehide
        })
    },
    switchSpeak: function() {
        this.setData({
            speak: !this.data.speak
        })
    },
    tostart: function() {
        this.setData({
            recodingClass: 'recoding'
        })
        this.startTime = new Date();
        var self = this;
        wx.startRecord({
            success: function(res) {
                var diff = (self.endTime - self.startTime) / 1000;
                diff = Math.ceil(diff)
                self.submitVoice({ url: res.tempFilePath, timeLen: diff });
            }
        })
    },
    toend: function() {
        this.setData({
            recodingClass: ''
        })
        this.endTime = new Date();
        wx.stopRecord()
    },
    submitVoice: function(obj) {
        var newData = {
            username: 'cxw',
            avatar: '/images/avatar-3.png',
            create_time: new Date().getTime() / 1000,
            content: {
                txt: '',
                img: [],
                audio: obj
            }
        };
        this.dbPost.newComment(newData);
        this.showSuccess();
        this.bindData();
    },
    showSuccess: function() {
        wx.showToast({
            title: '评论成功',
            duration: 1000,
            icon: "success"
        })
    },
    bindData: function() {
        var comments = this.dbPost.getCommentData();
        this.setData({
            comments: comments
        })
    },
    listenvoice: function(event) {
        var url = event.currentTarget.dataset.url;
        if (url == this.data.currentAudio) {
            wx.pauseVoice();
            this.data.currentAudio = '';
        } else {
            var self = this;
            wx.playVoice({
                filePath: url,
                complete: function() {
                    self.data.currentAudio = ''
                }
            })
        }
    },
    commentinput: function(event) {
        var val = event.detail.value;
        this.data.inputvalue = val;
    },
    toSubmit: function() {
        var imgs = this.data.chooseFiles;
        var newData = {
            username: 'cxw',
            avatar: '/images/avatar-3.png',
            create_time: new Date().getTime() / 1000,
            content: {
                txt: this.data.inputvalue,
                img: imgs
            },
        }
        if (newData.content.txt || newData.content.img.length) {
            this.dbPost.newComment(newData);
            this.showSuccess();
            this.bindData();
            this.back()
        }
    },
    back: function() {
        this.setData({
            morehide: true,
            inputvalue: '',
            chooseFiles: [],
        })
    },
    choosePic:function(event){
        var imgArr=this.data.chooseFiles;
        var leftCount = 3 - imgArr.length;
        if (leftCount <= 0) {
            return;
        }
        var sourceType=[event.currentTarget.dataset.type],
        self=this;
        wx.chooseImage({
            count:leftCount,
            sourceType:sourceType,
            success:function(res){
                self.setData({
                    chooseFiles:imgArr.concat(res.tempFilePaths)
                })
            }
        })
    },
    proview: function (event) {
    var src = event.currentTarget.dataset.src;
    wx.previewImage({
      current: src, 
      urls: [src] 
    })
  }

})